<?php

namespace app\controller;

use think\facade\Request;
use app\BaseController;
use app\ResourceModel;
use app\controller\ResourceController;
use think\facade\Db;

class ResourceManagementController extends ResourceController
{
    public function getArticleNameList(Request $request)
    {
        //获取文章名称列表
        $param = $request::param();
        $list = Db::table('shenbei_site_management_articles')->where('del_flag', 0)->field('article_name')->select()->toArray();
        return $this->returnJson(1001, '返回成功', ['list' => $list]);

    }

    public function addArticle(Request $request)
    {
        //添加文章
        $param = $request::post();
        $data['id'] = uniqid();
        $data['article_name'] = $param['article_name'];
        $data['article_author'] = $param['article_author'];
        $data['category_id'] = $param['category_id'];
        $data['create_time'] = toDate(time());
        $ret = Db::table('shenbei_site_management_articles')->insert($data);
        if ($ret != false) {
            return $this->returnJson(1001, '保存成功', $param);
        } else {
            return $this->returnJson(1002, '保存失败', ['data' => $data]);
        }
    }

    public function getArticleDetails(Request $request)
    {
        //通过id获取文章详情内容
        $param = $request::param('id');
        $rm = new ResourceModel();
        $list = $rm->table('shenbei_site_management_articles')
            ->alias('ssma')
            ->leftJoin(['article_category'=>'ac'],'ssma.category_id = ac.id')
            ->field('ssma.id,ssma.article_info,ssma.article_name,ssma.article_author,ssma.category_id,ssma.create_time,ac.title as category_name')
            ->where('ssma.del_flag', 0)
            ->order(['ssma.create_time'=>'desc','ssma.sort'=>'desc'])
            ->where('ssma.id', $param)
            ->select()
            ->toArray();
        return $this->returnJson(1001, '返回成功', ['list' => $list]);

    }

    public function updateArticle(Request $request)
    {
        //修改文章
        $param = $request::post();
        $data['article_name'] = $param['article_name'];
        $data['article_author'] = $param['article_author'];
        $data['category_id'] = $param['category_id'];
        $data['update_time'] = toDate(time());
        $ret = Db::table('shenbei_site_management_articles')->where('id', $param['id'])->update($data);
        if ($ret != false) {
            return $this->returnJson(1001, '修改成功');
        } else {
            return $this->returnJson(1002, '修改失败');
        }
    }

    public function deleteArticle(Request $request)
    {
        //根据id删除文章
        $param = $request::param();
        $result = Db::table('shenbei_site_management_articles')->where('id', $param['id'])->update(['del_flag' => 1]);
        if ($result != false) {
            return $this->returnJson(1001, '删除成功');
        } else {
            return $this->returnJson(1002, '删除失败');
        }
    }

    public function deleteData(Request $request)
    {
        //批量删除文章
        $param = $request::param();
        $result = Db::table('shenbei_site_management_articles')->whereIn('id', json_decode($param['ids']))->update(['del_flag' => 1]);
        if ($result != false) {
            return $this->returnJson(1001, '删除成功');
        } else {
            return $this->returnJson(1002, '删除失败');
        }
    }

    //文章管理列表分页接口
    public function getArticleListPage(Request $request)
    {
        $param = $request::param();
        $pageSize = $param['pageSize'];
        $rm = new ResourceModel();
        $list = $rm->table('shenbei_site_management_articles')
            ->alias('ssma')
            ->leftJoin(['article_category'=>'ac'],'ssma.category_id = ac.id')
            ->field('ssma.id,ssma.article_info,ssma.article_name,ssma.article_author,ssma.category_id,ssma.create_time,ac.title as category_name')
            ->where('ssma.del_flag', 0)
            ->order(['ssma.create_time'=>'desc','ssma.sort'=>'desc'])
            ->paginate($pageSize)
            ->toArray();
        return $this->returnPageJson(1001, '返回数据成功', $list);
    }
}
